using System;
using System.Runtime.InteropServices;

namespace Hardcodet.Wpf.TaskbarNotification.Interop
{
	/// <summary>
	/// A struct that is submitted in order to configure
	/// the taskbar icon. Provides various members that
	/// can be configured partially, according to the
	/// values of the <see cref="T:Hardcodet.Wpf.TaskbarNotification.Interop.IconDataMembers" />
	/// that were defined.
	/// </summary>
	public struct NotifyIconData
	{
		/// <summary>
		/// Size of this structure, in bytes.
		/// </summary>
		public uint cbSize;

		/// <summary>
		/// Handle to the window that receives notification messages associated with an icon in the
		/// taskbar status area. The Shell uses hWnd and uID to identify which icon to operate on
		/// when Shell_NotifyIcon is invoked.
		/// </summary>
		public IntPtr WindowHandle;

		/// <summary>
		/// Application-defined identifier of the taskbar icon. The Shell uses hWnd and uID to identify
		/// which icon to operate on when Shell_NotifyIcon is invoked. You can have multiple icons
		/// associated with a single hWnd by assigning each a different uID. This feature, however
		/// is currently not used.
		/// </summary>
		public uint TaskbarIconId;

		/// <summary>
		/// Flags that indicate which of the other members contain valid data. This member can be
		/// a combination of the NIF_XXX constants.
		/// </summary>
		public IconDataMembers ValidMembers;

		/// <summary>
		/// Application-defined message identifier. The system uses this identifier to send
		/// notifications to the window identified in hWnd.
		/// </summary>
		public uint CallbackMessageId;

		/// <summary>
		/// A handle to the icon that should be displayed. Just
		/// <see cref="!:Icon.Handle" />.
		/// </summary>
		public IntPtr IconHandle;

		/// <summary>
		/// String with the text for a standard ToolTip. It can have a maximum of 64 characters including
		/// the terminating NULL. For Version 5.0 and later, szTip can have a maximum of
		/// 128 characters, including the terminating NULL.
		/// </summary>
		[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
		public string ToolTipText;

		/// <summary>
		/// State of the icon. Remember to also set the <see cref="F:Hardcodet.Wpf.TaskbarNotification.Interop.NotifyIconData.StateMask" />.
		/// </summary>
		public IconState IconState;

		/// <summary>
		/// A value that specifies which bits of the state member are retrieved or modified.
		/// For example, setting this member to <see cref="F:Hardcodet.Wpf.TaskbarNotification.Interop.IconState.Hidden" />
		/// causes only the item's hidden
		/// state to be retrieved.
		/// </summary>
		public IconState StateMask;

		/// <summary>
		/// String with the text for a balloon ToolTip. It can have a maximum of 255 characters.
		/// To remove the ToolTip, set the NIF_INFO flag in uFlags and set szInfo to an empty string.
		/// </summary>
		[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
		public string BalloonText;

		/// <summary>
		/// Mainly used to set the version when <see cref="M:Hardcodet.Wpf.TaskbarNotification.Interop.WinApi.Shell_NotifyIcon(Hardcodet.Wpf.TaskbarNotification.Interop.NotifyCommand,Hardcodet.Wpf.TaskbarNotification.Interop.NotifyIconData@)" /> is invoked
		/// with <see cref="F:Hardcodet.Wpf.TaskbarNotification.Interop.NotifyCommand.SetVersion" />. However, for legacy operations,
		/// the same member is also used to set timouts for balloon ToolTips.
		/// </summary>
		public uint VersionOrTimeout;

		/// <summary>
		/// String containing a title for a balloon ToolTip. This title appears in boldface
		/// above the text. It can have a maximum of 63 characters.
		/// </summary>
		[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 64)]
		public string BalloonTitle;

		/// <summary>
		/// Adds an icon to a balloon ToolTip, which is placed to the left of the title. If the
		/// <see cref="F:Hardcodet.Wpf.TaskbarNotification.Interop.NotifyIconData.BalloonTitle" /> member is zero-length, the icon is not shown.
		/// </summary>
		public BalloonFlags BalloonFlags;

		/// <summary>
		/// Windows XP (Shell32.dll version 6.0) and later.<br />
		/// - Windows 7 and later: A registered GUID that identifies the icon.
		///   This value overrides uID and is the recommended method of identifying the icon.<br />
		/// - Windows XP through Windows Vista: Reserved.
		/// </summary>
		public Guid TaskbarIconGuid;

		/// <summary>
		/// Windows Vista (Shell32.dll version 6.0.6) and later. The handle of a customized
		/// balloon icon provided by the application that should be used independently
		/// of the tray icon. If this member is non-NULL and the <see cref="F:Hardcodet.Wpf.TaskbarNotification.Interop.BalloonFlags.User" />
		/// flag is set, this icon is used as the balloon icon.<br />
		/// If this member is NULL, the legacy behavior is carried out.
		/// </summary>
		public IntPtr CustomBalloonIconHandle;

		/// <summary>
		/// Creates a default data structure that provides
		/// a hidden taskbar icon without the icon being set.
		/// </summary>
		/// <param name="handle"></param>
		/// <returns></returns>
		public static NotifyIconData CreateDefault(IntPtr handle)
		{
			NotifyIconData notifyIconData = default(NotifyIconData);
			if (Environment.OSVersion.Version.Major >= 6)
			{
				notifyIconData.cbSize = (uint)Marshal.SizeOf(notifyIconData);
			}
			else
			{
				notifyIconData.cbSize = 504u;
				notifyIconData.VersionOrTimeout = 10u;
			}
			notifyIconData.WindowHandle = handle;
			notifyIconData.TaskbarIconId = 0u;
			notifyIconData.CallbackMessageId = 1024u;
			notifyIconData.VersionOrTimeout = 0u;
			notifyIconData.IconHandle = IntPtr.Zero;
			notifyIconData.IconState = IconState.Hidden;
			notifyIconData.StateMask = IconState.Hidden;
			notifyIconData.ValidMembers = IconDataMembers.Message | IconDataMembers.Icon | IconDataMembers.Tip;
			notifyIconData.ToolTipText = (notifyIconData.BalloonText = (notifyIconData.BalloonTitle = string.Empty));
			return notifyIconData;
		}
	}
}
